package com.testapp.bluetoothtest08.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSONObject;
import com.testapp.bluetoothtest08.Constant.Constants;
import com.testapp.bluetoothtest08.File.ChallengeFile;
import com.testapp.bluetoothtest08.File.CodeFile;
import com.testapp.bluetoothtest08.File.DeviceFile;
import com.testapp.bluetoothtest08.File.GenerateKeyFile;
import com.testapp.bluetoothtest08.File.Params;
import com.testapp.bluetoothtest08.File.ScanCodeFirstRequestFile;
import com.testapp.bluetoothtest08.File.ScanCodeSecondRequestFile;
import com.testapp.bluetoothtest08.File.Statement;
import com.testapp.bluetoothtest08.File.UserScanCodeDataFile;
import com.testapp.bluetoothtest08.Http.OkHttp;
import com.testapp.bluetoothtest08.R;

public class YellowRefreshDialog extends Dialog {
    private Button refresh_confirm,refresh_cancel;
    private EditText refresh_code;
    private String code;
    private View v;
    private TextView statement;
    public YellowRefreshDialog(@NonNull Context context, View view) {
        super(context);
        this.v = view;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yellow_refresh);
        init();
    }

    private void init() {
        refresh_cancel = findViewById(R.id.refresh_cancel);
        refresh_confirm = findViewById(R.id.refresh_confirm);
        refresh_code=findViewById(R.id.refresh_code);

        statement = v.findViewById(R.id.my_statement);

        refresh_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        refresh_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                code=refresh_code.getText().toString();
                if(code.equals("")){
                    code="0";
                }
                int c = Integer.parseInt(code);
                System.out.println(" 发送的 Json ----> "+JSONObject.toJSONString(c));
                OkHttp http = new OkHttp();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String re = http.httpPostWithToken(Constants.SEND_RECOVER_URL,JSONObject.toJSONString(c));
                        System.out.println("恢复 ----> 服务器返回 ----> "+re);
                        if(re!=null){
                            if(re.contains("成功")){
                                statement.setText("正常者");
                                new Statement().write("正常者");
                                DeviceFile deviceFile = new DeviceFile();
                                deviceFile.clearAll();

                                new ChallengeFile().clear();
                                new GenerateKeyFile().clear();
                                new ScanCodeFirstRequestFile().clear();
                                new ScanCodeSecondRequestFile().clear();
                                new UserScanCodeDataFile().clear();

                                System.out.println("设备清空 ---- > "+new DeviceFile().readAllDevice());
                                dismiss();
                                Looper.prepare();
                                Toast.makeText(getContext(), "状态已更新", Toast.LENGTH_SHORT).show();
                                Looper.loop();

                            }else{
                                Looper.prepare();
                                Toast.makeText(getContext(), "验证码错误", Toast.LENGTH_SHORT).show();
                                Looper.loop();
                            }
                        }else{
                            Looper.prepare();
                            Toast.makeText(getContext(), "验证码错误", Toast.LENGTH_SHORT).show();
                            Looper.loop();
                        }
                    }
                }).start();
            }
        });
    }
}
